<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="组织架构" class="tree-card">
            <tree-tools :tree-node="company" :is-root="true" style="font-weight: bold;border-bottom: 1px solid #cfcfcf;" @addDepts="addDepts" />
            <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
              <tree-tools slot-scope="{ data }" :tree-node="data" @delDepts="getDepartments" @addDepts="addDepts" @editDepts="editDepts" />
            </el-tree>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <add-dept ref="addDepts" :show-dialog.sync="showDialog" :tree-node="node" @addDepts="getDepartments" />
  </div>
</template>

<script>
import TreeTools from './components/tree-tools'
import AddDept from './components/add-dept'
import { getDepartments } from '@/api/departments'
import { transListToTreeData } from '@/utils'

export default {
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      // 定义数据模型
      showDialog: false, // 默认不显示窗体
      company: {},
      defaultProps: {
        // 指定节点的标签名称
        label: 'name'
      },
      departs: [],
      node: null // 当前节点
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      // 调用 api 接口
      const result = await getDepartments()
      this.company = { name: result.companyName, manager: '负责人', id: '' }
      this.departs = transListToTreeData(result.depts, '')
    },
    addDepts(node) {
      this.showDialog = true
      this.node = node
    },
    editDepts(node) {
      this.showDialog = true
      this.node = node
      console.log(node.id)
      // 父组件调用子组件的 getDepartDetail 方法
      this.$refs.addDepts.getDepartDetail(node.id)
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
